/*
 * Copyright 2011 Helios Technologies.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.heliostech.gwt.widget.dialog.msg;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.CssResource.ImportedWithPrefix;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.heliostech.gwt.widget.dialog.base.BaseDialog;

/**
 * @author ldesdier
 *
 */
public class MessageDialog extends BaseDialog {

	/**
	 * Severity level of the displayed message.
	 * @author ldesdier
	 */
	public enum Level {
		INFO(0),
		WARNING(1),
		ERROR(2);
		
		private int level;
		
		private Level(int l) {
			level = l;
		}
		
		public int getLevel() {
			return level;
		}
	}
	
	protected interface Resources extends ClientBundle {
		@Source("./info.png")
		ImageResource infoIcon();
		@Source("./warning.png")
		ImageResource warningIcon();		
		@Source("./error.png")
		ImageResource errorIcon();		
		@Source(Style.DEFAULT_CSS)
		@CssResource.NotStrict
	    Style msgDialogStyle();
	}
	
	@ImportedWithPrefix("helios-MessageDialog")
	public interface Style extends CssResource {
		String DEFAULT_CSS = "com/heliostech/gwt/widget/dialog/msg/MessageDialog.css";
		
		String msgDialogWidget();
		
		String msg();

		String closeButton();
		
	}
	
	private static Resources DEFAULT_RESOURCES;
	private static Resources getDefaultResources() {
		if (DEFAULT_RESOURCES == null) {
			DEFAULT_RESOURCES = GWT.create(Resources.class);
		}
		return DEFAULT_RESOURCES;
	}	
	private final Style style;
	private final Resources resources;
	private final HTML msg;
	
	public MessageDialog() {
		this(true);
	}
	
	public MessageDialog(boolean modal) {
		this(modal, getDefaultResources());
	}
	
	public MessageDialog(boolean modal, MessageDialog.Resources resources) {
		super("", true, modal);
		this.resources = resources;
		this.style = resources.msgDialogStyle();
		this.style.ensureInjected();
		addStyleName(style.msgDialogWidget());
		
		msg = new HTML();
		setContent(msg);
		
		final Button closeB = new Button("Close");
		closeB.addStyleName(style.closeButton());
		closeB.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hide();
			}
		});
		addButton(closeB);
	}
	
	/**
	 * Set the severity level of the displayed message.
	 * @param l The severity level
	 */
	public void setLevel(Level l) {
		if(Level.INFO.equals(l)) {
			setDialogTitle("Information");
			setDialogDescription("Information", 
					"Click on the [Close] button to dismiss this message", resources.infoIcon());
		} else if (Level.WARNING.equals(l)) {
			setDialogTitle("Warning");
			setDialogDescription("Warning", 
					"Click on the [Close] button to dismiss this message", resources.warningIcon());
		} else if (Level.ERROR.equals(l)) {
			setDialogTitle("Error");
			setDialogDescription("Error", 
					"Click on the [Close] button to dismiss this message", resources.errorIcon());
		} else {
			throw new RuntimeException("Invalid message severity level.");
		}
	}
	
	/**
	 * Set the message to be displayed as the dialog content.
	 * @param m
	 */
	public void setMessage(String m) {
		this.msg.setHTML(SafeHtmlUtils.fromSafeConstant(m));
	}
	
	/**
	 * Set the message to be displayed as the dialog content and its severity level.
	 * @param m
	 * @param l
	 */
	public void setMessage(String m, Level l) {
		setLevel(l);
		setMessage(m);
	}
	
}
